﻿using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Action;
using Obsidian.Utils;
using Oak.Base;
using Oak.Model;
using Oak.MagSys.Logic;

namespace Oak.MagSys.Serv
{
    public class AdminIconServ : OakServ<ISession>
    {
        private const string FM_ADMIN_ICON = "itemId,adminId,iconId,created";
        private const string FN_ICON = "iconId,parentId,frameId,depth,iconImg,text,isSystemIcon,sortOrder";

        public AdminIconServ(ISession session) : base(session) { }

        public void Update(ActionRequest req, ActionResponse res)
        {

            ActReqParam param = req.GetParamByNameOrFirst("params");
            if (param == null)
            {
                res.Error("参数item错误");
                return;
            }

            long adminId;
            if (!param.TryGetFirstLong("adminId", out adminId))
            {
                res.Error("参数item属性adminId错误");
                return;
            }

            string strIconIds;
            if (!param.TryGetFirstString("iconIds", out strIconIds))
            {
                res.Error("参数item属性iconIds错误");
                return;
            }
            long[] iconIds = ArrayUtil.SplitInt64(strIconIds, ',');
            AdminIconLG iconLG = new AdminIconLG(Session);
            string msg;
            bool succ = iconLG.UpdateAdminIconsByAdminId(adminId, iconIds, out msg);
            if (!succ)
            {
                res.Error(msg);
                return;
            }

        }

        public void List(ActionRequest req, ActionResponse res)
        {
            MsAdminIconQry qry = req.GetModelByNameOrFirst<MsAdminIconQry>("qry");

            AdminIconLG iconLG = new AdminIconLG(Session);
            string msg;
            List<MsAdminIconInfo> list = iconLG.GetAdminIconList(qry, out msg);
            if (list == null)
            {
                res.Error(msg);
                return;
            }

            res.AddResult("items", FM_ADMIN_ICON, list)
                .SetRelation("icon", "iconId", "icons", "iconId");
            res.AddResult("icons", FN_ICON, "iconId", "icon", list);

        }

        public void Listing(ActionRequest req, ActionResponse res)
        {
            AdminIconLG iconLG = new AdminIconLG(Session);
            string msg;
            List<MsIconInfo> list = iconLG.ListingAdminIcons(out msg);
            if (list == null)
            {
                res.Error(msg);
                return;
            }

            res.AddResult("icons", FN_ICON, list);
        }

    }
}
